-- ====================================
-- Global Orunj table and core functions
-- used to record player data.
-- ====================================
Orunj = Orunj or {} -- The table to store everything.
Orunj.frame = CreateFrame("Frame", "OrunjFrame") -- Create the event handling frame.
Orunj.recording = false -- Flag used to see if we are recording data or not.
Orunj.cmdprefix = "[ORUNJ] " -- A prefix to indicate that the addon is talking to you in-game.
Orunj.inspectEnabled = false -- If it is set to true, then the OnUpdate function will call Orunj.InspectPlayers.

-- ====================================
-- Constructors
-- ====================================
-- Start a new OrunjSession.
function Orunj:NewSession()
	-- Identify the snapshot with the current date.
	-- [http://www.wowwiki.com/API_date]
	local snapshotID = date("%m/%d/%y %H:%M:%S")
	
	-- Create a new key in OrunjSave using the game time/date, to ensure uniqueness.
	OrunjSave.sessions[snapshotID] = {}
	self.session = OrunjSave.sessions[snapshotID]
	self.session.globalTimelines = { mapTimeline = {} } -- Probably will have to add more stuff in here later.
	self.session.players = {}
	self.session.sequenceNumber = 1
	self.session.timestep = 1 -- in seconds.
end

-- Returns a new player entry using the unitId
function Orunj:NewOrunjPlayer(unitId)
	-- We are going ot use the englishClass name to refer to the
	-- player's classes.
	-- [http://www.wowwiki.com/API_UnitClass]
	localizedClass, englishClass = UnitClass(unitId)
	return { 
			unitId = unitId,
		 	inspected = false, 
		 	name = UnitName(unitId), 
		 	me = unitId == "player",
		 	maxHealth = 1,
		 	maxPower = 1,
		 	class = englishClass,
			talentTrees = {}, 
			role = role, 
			timelines = {	unitHealthTimeline = {},
							--unitHealthMaxTimeline = {},
							unitPowerTimeline = {},
							--unitPowerMaxTimeline = {},
							xPositionTimeline = {},
							yPositionTimeline = {}
					    }
		   }
end

-- Function to toggle recording data.
function Orunj:ToggleRecording()
	if not self.recording then
		self:StartRecording()
	else
		self:StopRecording()
	end
end

-- Start Recording.
function Orunj:StartRecording()
	print(Orunj.cmdprefix .. "Start recording")
	
	-- Start a new session.
	self:NewSession()
	
	-- Create a new snapshot and store it in OrunjSave
	self:TakeSnapshot() -- OrunjInspect.lua

	-- Take initial note of the current map.
	self:UpdateGlobalTimelineMap() -- OrunjTimeline.lua

	-- Make as many icons as required in the GUI.
	self:InitializeIcons() -- OrunjGUI.lua

	-- Start recording the chat log
	if not LoggingChat() then
		LoggingChat(true)
		print(Orunj.cmdprefix .. "> Recording chat log.")
	end
	
	-- Start recording the combat log
	if not LoggingCombat() then
		LoggingCombat(true)
		print(Orunj.cmdprefix .. "> Recording combat log.")
	end
	
	-- Set the recording state to true.	
	self.recording = true
end

-- Stop Recording.
function Orunj:StopRecording()
	print(Orunj.cmdprefix .. "Stop recording")
	
	-- Stop recording the combat log
	if LoggingChat() then
		LoggingChat(false)
		print(Orunj.cmdprefix .. "> Stopped recording chat log.")
	end
	
	-- Stop recording the chat log.
	if LoggingCombat() then
		LoggingCombat(false)
		print(Orunj.cmdprefix .. "> Stopped recording combat log.")
	end
	
	self.recording = false
end


